<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <title>title</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css" />
    <style>
        body {}
    </style>
</head>

<body>
<hr>

    <!--上传执照 -->
    <div class="address-name">
        <div>
            <span style="font-size:14px;color:#333333">上传营业执照</span>
        </div>
    </div>
    <div class="licence-all" tapmode onclick="fnOpenMedia();">
        <img id="licencePic" src="../image/m-camera.png" alt="" style="width:60px;height:60px;border-radius: 30px;"
            class="la-img">
    </div>

</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript">
    //1.1 打开拍照功能或者媒体库 
    function fnOpenMedia() {
        api.actionSheet({
            // title: '', 
            cancelTitle: '取消',
            buttons: ['拍照', '从手机相册选择']
        }, function (ret, err) {
            getPicture(ret.buttonIndex);
        });
    }

    // 1.2 两种方式上传图片 
    function getPicture(index_) {
        // 拍照 
        if (index_ == 1) {
            api.getPicture({
                sourceType: 'camera', // 拍照 
                encodingType: 'jpg',
                mediaValue: 'pic',
                destinationType: 'url',
                allowEdit: false,
                quality: 90,
                saveToPhotoAlbum: false
            }, function (ret, err) {
                if (ret.data == '' || ret.data == null) {
                    return;
                } else {
                    // alert(JSON.stringify(ret.data)); 
                    // 获取到图片将图片上传到服务器 
                    api.ajax({
                        //url: util.url.uploadFile,
                        url: 'http://111.231.31.70:8080/upload/UploadImg',
                        method: 'POST',
                        name: 'file',
                        dataType:'JSON',
                        charset:'utf-8',
                        headers: {
                            "Content-Type": "multipart/form-data",
                            "token":"admin"
                        },
                        files:{
                            file:ret.data
                        },
                        formData: {
                            filetype: 'image',
                            file: ret.data
                        }
                    }, function (ret, err) {
                        console.log(ret);
                        console.log(err);
                        if (ret) {
                            // 给vue中的 
                            app.businessLicense = util.ctxt + ret.fileName;
                            // alert(JSON.stringify(app.imageNameV )); 
                        } else {
                            alert(JSON.stringify(err));
                        }
                    });
                    $api.attr($api.byId('licencePic'), 'src', ret.data);
                    $api.attr($api.byId('licencePic'), 'style', "width:100%;height:100%;");
                }
            });

            // 从手机相册选取 
        } else if (index_ == 2) {
            api.getPicture({
                sourceType: 'album',
                encodingType: 'jpg',
                mediaValue: 'pic',
                destinationType: 'url',
                // destinationType: 'base64', 
                allowEdit: false,
                quality: 90,
                saveToPhotoAlbum: true
            }, function (ret, err) {
                if (ret.data == '' || ret.data == null) {
                    return;
                } else {
                    console.log(JSON.stringify(ret.data));
                    api.ajax({
                        url: 'http://111.231.31.70:8080/upload/UploadImg',
                        method: 'post',
                        dataType:'JSON',
                        data:{
                            files:{
                                file:ret.data
                            }
                        }
                        ,
                        formData: {
                            filetype: 'image',
                            file: ret.data
                        }
                    }, function (ret, err) {
                        console.log(ret);
                        console.log(err);
                        console.log(JSON.stringify(err));
                        if (ret) {
                            //{"data":{"fileName":"file","newName":"1608618470444.jpg",
                            //"filePath":"/www/wwwroot/javaweb/upload//2020/12/1608618470444.jpg",
                            //"proPath":"/upload//2020/12/1608618470444.jpg","fileSize":3003293,"fileType":".jpg"},
                            //"code":"0","msg":"上传成功","pageUtil":""}

                            console.log(JSON.stringify(ret)); 
                        } else {
                            alert(JSON.stringify(err));
                        }
                    });
                    $api.attr($api.byId('licencePic'), 'src', ret.data);
                    $api.attr($api.byId('licencePic'), 'style', "width:100%;height:100%;");
                }
            });
        }
    }
    apiready = function () {

    };
</script>

</html>